package com.zhoug.ocr.textin.parser;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.ocr.textin.data.FormResult;
import com.zhoug.ocr.textin.data.FormTable;
import com.zhoug.ocr.textin.data.HeaderField;
import com.zhoug.ocr.textin.data.TableCell;

import java.util.List;

/**
 * 默认的form表单数据Json格式化
 *
 * @Author: zhoug
 * @Date: 2023/3/1
 * @Description:
 */
public class DefaultFormResultParser extends BaseFormResultParser {

    public DefaultFormResultParser() {
    }

    /**
     * 把表单数据转化为json数组
     *
     * @param formResult
     * @param headerFields
     * @param byHeaderOrder
     * @return
     */
    @Override
    public String parseFormResult(FormResult formResult, List<HeaderField> headerFields, boolean byHeaderOrder) {
        if (DEBUG) {
            LogUtils.d(TAG, "parseFormResult:formResult=" + formResult);
        }
        setHeaderDefaultField(headerFields);
        FormResult formResult1 = filterEmptyRowCol(formResult);
        List<HeaderField> headers = parseHeader(formResult1, headerFields);
        if (byHeaderOrder) {
            headers = parseHeaderByOrder(headers, headerFields);
        }
        JsonArray jsonArray = parseLine(formResult, headers);
        return jsonArray != null ? jsonArray.toString() : null;
    }

    /**
     * 取出表头对应的列组成json数组
     *
     * @param formResult
     * @param headers
     * @return
     */
    public JsonArray parseLine(FormResult formResult, List<HeaderField> headers) {
        JsonArray jsonArray = null;
        if (headers != null && headers.size() > 0) {
            if (formResult != null) {
                List<FormTable> tables = formResult.getTables();
                if (tables != null && tables.size() > 0) {
                    jsonArray = new JsonArray();
                    for (int tableIndex = 0; tableIndex < tables.size(); tableIndex++) {
                        FormTable formTable = tables.get(tableIndex);
                        if (formTable != null) {
                            List<TableCell> table_cells = formTable.getTable_cells();
                            if (table_cells != null && table_cells.size() > 0) {
                                for (TableCell item : table_cells) {
                                    String cellText = item.getText();
                                    int start_row = item.getStart_row();
                                    int end_row = item.getEnd_row();
                                    int start_col = item.getStart_col();
                                    int end_col = item.getEnd_col();
                                    for (HeaderField header : headers) {
                                        if (header.getStartCol() == start_col && header.getEndCol() == end_col && header.getEndRow() < start_row) {
                                            JsonObject jsonObject = getJsonObject(jsonArray, start_row, end_row);
                                            jsonObject.addProperty(header.getField(), cellText);
                                        }

                                    }
                                }


                            }
                        }
                    }
                }
            }
        }
        return jsonArray;
    }

}
